<template>
	<view class="container">
		<uni-section title="表头信息" type="line">
			<view class="example">

				<!-- 展示不同的排列方式 -->
				<uni-forms ref="baseForm" :modelValue="baseData" :rules="rules">
					<uni-forms-item label="单号" required name="RecodNum">
						<uni-easyinput v-model="baseData.RecodNum" placeholder="请输入单号" />
					</uni-forms-item>
					<uni-forms-item label="日期" required name="RecodDate">
						<uni-datetime-picker type="date" :clear-icon="false" v-model="baseData.RecodDate"
							placeholder="请输入日期" />
					</uni-forms-item>
					<uni-forms-item label="单据类型" required name="RecodTypeId">
						<uni-data-select v-model="baseData.RecodTypeId" :localdata="RecodType"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="仓库" required name="WarehouseId">
						<uni-data-select v-model="baseData.WarehouseId" :localdata="Warehouse"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="工单号" required name="OrderNum">
						<uni-easyinput v-model="baseData.OrderNum" placeholder="请输入工单号" />
					</uni-forms-item>
					<uni-forms-item label="收发类别" required name="SendReceiveId">
						<uni-data-select v-model="baseData.SendReceiveId" :localdata="SendReceive"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="供应商" required name="SupplierId">
						<uni-data-select v-model="baseData.SupplierId" :localdata="Supplier"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="备注">
						<uni-easyinput v-model="baseData.Description" placeholder="请输入备注" />
					</uni-forms-item>
				</uni-forms>
				<button type="primary" @click="submit('baseForm')">保存</button>
			</view>
		</uni-section>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseData: {
					RecodNum: 'I' + this.$dateformat.dateFormat(Date.now()),
					RecodDate: Date.now(),
					RecodTypeId: '',
					WarehouseId: '',
					OrderNum: '',
					SupplierId: '',
					SendReceiveId: '',
					Description: '',
					PdaCreatorUserName: this.$store.state.userInfo.RealName,
					PdaCreatorUserId: this.$store.state.userInfo.RealId,
				},
				rules: {
					// 对name字段进行必填验证
					RecodNum: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写单号',
							},

						],

					},
					RecodDate: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写日期',
							},

						],

					},
					RecodTypeId: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写单据类型',
							},

						],

					},
					WarehouseId: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写仓库',
							},

						],

					},
					OrderNum: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写工单号',
							},

						],

					},
					SupplierId: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写供应商',
							},

						],

					},
					SendReceiveId: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写收发类别',
							},

						],

					}
				},
				RecodType: [],
				Warehouse: [],
				SendReceive: [],
				Supplier: [],
			}
		},
		onLoad() {
			this.$api.Request({
				url: "/api/BasicData/GetWarehouse",
				method: 'GET',
				data: {},
			}).then((res) => {

				res.forEach(item => {

					this.Warehouse.push({
						value: item.Id,
						text: item.WarehouseName
					})

				});

			})
			this.$api.Request({
				url: "/api/BasicData/GetRecodType",
				method: 'GET',
				data: {},
			}).then((res) => {

				res.forEach(item => {

					this.RecodType.push({
						value: item.Id,
						text: item.DocumentstypeName
					})

				});

			})
			this.$api.Request({
				url: "/api/BasicData/GetSendReceive",
				method: 'GET',
				data: {},
			}).then((res) => {

				res.forEach(item => {

					this.SendReceive.push({
						value: item.Id,
						text: item.SendReceiveName
					})

				});

			})
			this.$api.Request({
				url: "/api/BasicData/GetSuppliers",
				method: 'GET',
				data: {},
			}).then((res) => {

				res.forEach(item => {

					this.Supplier.push({
						value: item.Id,
						text: item.SupplierName
					})

				});

			})

		},
		methods: {
			submit(ref) {
				this.baseData.RecodDate = this.$dateformat.dateFormat(this.baseData.RecodDate, 'date');
				this.$refs[ref].validate().then(res => {
					this.$api.Request({
						url: "/api/Input/SubmitFormHead",
						method: 'POST',
						data: this.baseData,
					}).then((res) => {
						if (res.state == 'success') {
							uni.showToast({
								title: `提交成功`,
								
							})
							setTimeout(()=>{
								uni.navigateTo({
								url: '../inputheadlist/inputheadlist'
							  })	
							},1000)
							
						} else {

							uni.showToast({
								title: `提交失败`,
								icon:'error',
							})
						}
					})

				}).catch(err => {
					console.log('err', err);
				})
			},
			
		onNavigationBarButtonTap() {  
		    this.submit('baseForm');
		    } 
		}
	}
</script>

<style>

</style>
